package com.ruoyi.a2.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.a2.domain.A2DynamicsInfo;
import com.ruoyi.a2.domain.A2StaticInfo;
import com.ruoyi.a2.dto.A2HPMConfigCondition;
import com.ruoyi.a2.dto.A2HPMConfigForm;
import com.ruoyi.a2.dto.A2HPMConfigJson;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.Date2Utils;
import com.ruoyi.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.a2.mapper.A2HPMConfigMapper;
import com.ruoyi.a2.domain.A2HPMConfig;
import com.ruoyi.a2.service.IA2HPMConfigService;
import org.springframework.util.CollectionUtils;

/**
 * 历史规律挖掘配置Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-17
 */
@Slf4j
@Service
public class A2HPMConfigServiceImpl extends ServiceImpl<A2HPMConfigMapper, A2HPMConfig> implements IA2HPMConfigService
{
    @Autowired
    private A2HPMConfigMapper a2HPMConfigMapper;

    /**
     * 查询历史规律挖掘配置
     *
     * @param id 历史规律挖掘配置主键
     * @return 历史规律挖掘配置
     */
    @Override
    public A2HPMConfig selectA2HPMConfigById(Long id)
    {
        return a2HPMConfigMapper.selectA2HPMConfigById(id);
    }

    /**
     * 查询历史规律挖掘配置列表
     *
     * @param a2HPMConfig 历史规律挖掘配置
     * @return 历史规律挖掘配置
     */
    @Override
    public List<A2HPMConfig> selectA2HPMConfigList(A2HPMConfig a2HPMConfig)
    {
        return a2HPMConfigMapper.selectA2HPMConfigList(a2HPMConfig);
    }

    /**
     * 新增历史规律挖掘配置
     *
     * @param a2HPMConfig 历史规律挖掘配置
     * @return 结果
     */
    @Override
    public int insertA2HPMConfig(A2HPMConfig a2HPMConfig)
    {
        a2HPMConfig.setCreateTime(DateUtils.getNowDate());
        return a2HPMConfigMapper.insertA2HPMConfig(a2HPMConfig);
    }

    /**
     * 修改历史规律挖掘配置
     *
     * @param a2HPMConfig 历史规律挖掘配置
     * @return 结果
     */
    @Override
    public int updateA2HPMConfig(A2HPMConfig a2HPMConfig)
    {
        a2HPMConfig.setUpdateTime(DateUtils.getNowDate());
        return a2HPMConfigMapper.updateA2HPMConfig(a2HPMConfig);
    }

    /**
     * 批量删除历史规律挖掘配置
     *
     * @param ids 需要删除的历史规律挖掘配置主键
     * @return 结果
     */
    @Override
    public int deleteA2HPMConfigByIds(Long[] ids)
    {
        return a2HPMConfigMapper.deleteA2HPMConfigByIds(ids);
    }

    /**
     * 删除历史规律挖掘配置信息
     *
     * @param id 历史规律挖掘配置主键
     * @return 结果
     */
    @Override
    public int deleteA2HPMConfigById(Long id)
    {
        return a2HPMConfigMapper.deleteA2HPMConfigById(id);
    }

    @Override
    public TableDataInfo page(A2HPMConfigCondition condition) {
        QueryWrapper<A2HPMConfig> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(condition.getTaskName())){
            wrapper.like("task_name", condition.getTaskName());
        }
        if(condition.getEnableStatus()!=null){
            wrapper.eq("enable_status", condition.getEnableStatus());
        }
        if(StringUtils.isNotBlank(condition.getFocusTarget())){
            wrapper.like("focus_target", condition.getFocusTarget());
        }
        wrapper.orderByDesc("id");
        IPage<A2HPMConfig> page = page(new Page<>(condition.getPageNum(), condition.getPageSize()), wrapper);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(page.getRecords());
        tableDataInfo.setTotal(page.getTotal());
        return tableDataInfo;
    }

    @Override
    public A2HPMConfig selectA2StaticInfoByTaskName(String taskName) {
        A2HPMConfig a2HPMConfig = new A2HPMConfig();
        a2HPMConfig.setTaskNameAccurate(taskName);
        List<A2HPMConfig> list = this.selectA2HPMConfigList(a2HPMConfig);
        if(!CollectionUtils.isEmpty(list)){
            return list.get(0);
        }
        return null;
    }

    @Override
    public AjaxResult saveOrUpdate(A2HPMConfigForm form) {
        A2HPMConfig bean = null;
        try {
            A2HPMConfig newBean = this.selectA2StaticInfoByTaskName(form.getTaskName());
            if (form.getId() != null) {
                bean = this.selectA2HPMConfigById(form.getId());
                if (bean == null) {
                    return AjaxResult.error("id错误，没有查询到对应数据！");
                }
                if (newBean != null && !newBean.getTaskName().equals(bean.getTaskName())) {
                    return AjaxResult.error("任务名称不能重复！");
                }
                bean.setUpdateTime(new Date());
            } else {
                if (newBean != null) {
                    return AjaxResult.error("任务名称不能重复！");
                }
                bean = new A2HPMConfig();
            }
            BeanUtils.copyProperties(form, bean);
            if(StringUtils.isNotBlank(form.getAnalysisStartTime())){
                bean.setAnalysisStartTime(Date2Utils.strToDateTime(form.getAnalysisStartTime()));
            }
            if(StringUtils.isNotBlank(form.getAnalysisEndTime())){
                bean.setAnalysisEndTime(Date2Utils.strToDateTime(form.getAnalysisEndTime()));
            }
            saveOrUpdate(bean);
        } catch (Exception e) {
            String errorMsg = "保存失败，请联系管理员！";
            log.error("{}|{}",errorMsg,e);
            return AjaxResult.error(errorMsg);
        }
        return AjaxResult.success();
    }

    @Override
    public void updateEnableStatus(A2HPMConfigJson json) {
        a2HPMConfigMapper.updateEnableStatus(json);
    }
}
